package blsX

import bls12381 "github.com/kilic/bls12-381"

var domainG2 = []byte("BLS12381G2_XMD:SHA-256_SSWU_RO_STARLINK")

// GetPublicKey 根据私钥，获取公钥
func GetPublicKey(secretKey [32]byte) []byte {
	var secret bls12381.Fr
	secret.FromBytes(secretKey[:])
	G1 := bls12381.NewG1()
	gen := G1.One()
	G1.MulScalar(gen, gen, &secret)
	return G1.ToCompressed(gen)
}

// SignMsg 对消息签名
func SignMsg(secret []byte, msg []byte) *bls12381.PointG2 {
	var G2 bls12381.G2
	tmpG2, err := G2.HashToCurve(msg, domainG2)
	if nil != err {
		panic(err)
	}
	var secretFr bls12381.Fr
	secretFr.FromBytes(secret[:])
	G2.MulScalar(tmpG2, tmpG2, &secretFr)
	return tmpG2
}

// AggregateSign 聚合签名
func AggregateSign(signs []*bls12381.PointG2) *bls12381.PointG2 {
	G2 := bls12381.NewG2()
	sum := G2.New()
	for _, p2 := range signs {
		G2.Add(sum, sum, p2)
	}
	return sum
}

// AggregatePub 公钥聚合
func AggregatePub(pubs []*bls12381.PointG1) *bls12381.PointG1 {
	G1 := bls12381.NewG1()
	sum := G1.New()
	for _, p1 := range pubs {
		G1.Add(sum, sum, p1)
	}
	return sum
}
